<?php


    class WxnavAction extends CommonAction{
	
	   public function index(){
	   
	     
	     $cat=M('nav')->select();
		 $re=$this->sel($cat);
	     $this->re=$re;
	     $this->display();
	   }
	   
	   public function addtop(){
	     if(!empty($_POST)){
		    $count=M('nav')->where('cid=0')->count();
			if($count>=3){
			
			  $this->error('顶级栏目最多只能添加三个');
			}
		 if(M('nav')->add($_POST)){
			
			  $this->success('添加成功',U('index'));
			}else{
			
			  $this->error('添加失败');
			}
		 
		 }	   
	     $this->display();
	   }
	   
	   public function addson(){
	   
	     if(!empty($_POST)){
		   
		    $count=M('nav')->where('cid="%s"',$_GET['id'])->count();
			if($count>=5){
			  $this->error('子菜单不能超过5个');
			}
		   
		 
		   $_POST['cid']=$_GET['id'];
		 
		   if(M('nav')->add($_POST)){
			
			  $this->success('添加成功',U('index'));
			}else{
			
			  $this->error('添加失败');
			}
		 
		 }
	   
	   
	   
	    $this->top=M('nav')->where('id="%s"',$_GET['id'])->find();
	   
	     $this->display();
	   }
	   
	   
	   public function del(){
	     
		 if(!empty($_POST)){
		 
		   $ishaveson=M('nav')->where('cid="%s"',$_POST['id'])->find();
		   
		   if($ishaveson!=null){
		     
			 $ajax['msg']=3;
		   
		   }else{
		   
		      $re=M('nav')->where('id="%s"',$_POST['id'])->delete();
		      
			  if($re){
			    $ajax['msg']=1;
			  }else{
			    $ajax=2;
			  }
		   
		   }
		   
		   $this->ajaxReturn($ajax);
		 
		 }
	   
	   
	   }
	   
	   
	   
	   public function savesort(){
	   
	      //dump($_POST);
		  $arr['sort']=$_POST['sort'];
		  $re=M('nav')->where('id="%s"',$_POST['id'])->save($arr);
		  
		  //last('nav');
		  if($re!==false){
		    $ajax=1;
		  }else{
		    $ajax=2;
		  }
	   
	      echo $ajax;
	   
	   }
	   
	   
	   
	   public function save(){
	     
		 $where['id']=array('eq',$_GET['id']);
	     if(!empty($_POST)){
		 
		   $re=M('nav')->where($where)->save($_POST);
		   
		   if($re!==false){
		   
		     $this->success('修改成功',U('index'));
			 exit();
		   }else{
		   
		     $this->error('失败');
		   
		   }
		 
		 
		 }
		 
	     $this->re=M('nav')->where($where)->find();
	     $this->display();
	   
	   }
	
	
	}